<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    okText="确定"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="行业">
          <a-input placeholder="请输入行业" v-decorator="['Industry', validatorRules.industry]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级行业">
          <a-select placeholder="请输入上级行业" v-decorator="['UpIndustryId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option v-if="model.BaseIndustryId !== item.BaseIndustryId" :value="item.BaseIndustryId" v-for="(item,index) in industryData">{{item.Industry}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="排序">
          <a-input placeholder="请输入排序" v-decorator="['OrderNum']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "BaseIndustryModal",
    watch:{
      visible(newVal,oldVal){
        if(newVal){

        }
      }
    },
    data () {
      return {
        industryData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        baseindustryid:{rules: [{ required: true, message: '请输入行业ID!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        industry:{rules: [{ required: true, message: '请输入行业!' }]},
        industryid:{rules: [{ required: true, message: '请输入!' }]},
        upindustryid:{rules: [{ required: true, message: '请输入上级行业!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        updatedate:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        },
        url: {
          add: "/baseIndustry/add",
          edit: "/baseIndustry/update",
        },
      }
    },
    created () {
      this.getIndustry();
    },
    methods: {
      getIndustry(){
        this.$http.postAction('/baseIndustry/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.industryData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.UpIndustryId = this.model.UpIndustry;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'UpIndustryId','Industry', 'OrderNum'))
		  //时间格式化
      // this.industryData = this.industryData.filter((e)=>{
      //   return e.BaseIndustryId !== this.model.BaseIndustryId
      // })
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.BaseIndustryId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>